

#include "CReadFile.h"
#include <stdio.h>
#include "os.h"

#ifdef _IRR_PSP_
#include <string.h>
#include <sys/types.h>
#include <sys/unistd.h>
#include <pspiofilemgr.h>
#endif

#ifdef _IRR_PSP_
//#include "pspdebug.h"
//#define printf pspDebugScreenPrintf
#endif

namespace irr
{
    namespace io
    {


        CReadFile::CReadFile(const c8* fileName)
            : FileSize(0)
        {
#ifdef _DEBUG
            setDebugName("CReadFile");
#endif
            Filename = fileName;
            openFile();
        }



        CReadFile::~CReadFile()
        {
            if (File)
            {
#ifdef _IRR_PSP_
                sceIoClose(File);
#else
                fclose(File);
#endif 
            }       
        }



        //! returns if file is open
        inline bool CReadFile::isOpen()
        {
            bool result = false;
#ifdef _IRR_PSP_
            result = File > -1;
#else
            result = File != 0;
#endif
            return result;
        }


        //! returns how much was read
        s32 CReadFile::read(void* buffer, s32 sizeToRead)
        {
            s32 result = 0;
           
			if (!isOpen())
			{
				return result;
			}
			
#ifdef _IRR_PSP_
            result = sceIoRead(File, buffer, sizeToRead);
#else
            result = fread(buffer, 1, sizeToRead, File);
#endif

            return result;
        }



        //! changes position in file, returns true if successful
        //! if relativeMovement==true, the pos is changed relative to current pos,
        //! otherwise from begin of file
        bool CReadFile::seek(s32 finalPos, bool relativeMovement)
        {
            bool result = false;
            if (!isOpen())
                return result;
#ifdef _IRR_PSP_
            result = sceIoLseek(File, finalPos, relativeMovement ? PSP_SEEK_CUR : PSP_SEEK_SET) == 0;
#else
            result = fseek(File, finalPos, relativeMovement ? SEEK_CUR : SEEK_SET) == 0; 
#endif
            return result;
        }



        //! returns size of file
        s32 CReadFile::getSize()
        {
            return FileSize;
        }



        //! returns where in the file we are.
        s32 CReadFile::getPos()
        {
            s32 result = 0;

            if (!isOpen())
                return result;

#ifdef _IRR_PSP_
            result = sceIoLseek(File, 0, PSP_SEEK_CUR);
#else
            result = ftell(File);
#endif
            return result;
        }



        //! opens the file
        void CReadFile::openFile()
        {

#ifdef _IRR_PSP_
            if (Filename.size() == 0) // bugfix posted by rt
            {
                File = -1;
                return;
            }

            c8 *FilePath;
            c8 *RealPath;
            core::stringc file_name;

            FilePath = (c8*)Filename.c_str();

            if (FilePath[0]== '/')
            {
                file_name = "ms0:";
                file_name += FilePath;
                RealPath = (c8*)file_name.c_str();

            }
            else
            {
                if (strstr(FilePath, ":/") == 0)
                {
                    c8 WorkingDirectory[1024];
                    getcwd(WorkingDirectory, 1024);

                    if (WorkingDirectory[ strlen(WorkingDirectory) - 1 ] != '\\' &&
                        WorkingDirectory[ strlen(WorkingDirectory) - 1 ] != '/')
                    {

                        WorkingDirectory[ strlen(WorkingDirectory) + 1 ] = 0;
                        WorkingDirectory[ strlen(WorkingDirectory) ] = '/';

                    }


                    file_name = WorkingDirectory;
                    file_name += FilePath;
                    RealPath = (c8*)file_name.c_str();
                }
                else
                {
                    RealPath = FilePath;
                }

            }

			File = sceIoOpen(RealPath, PSP_O_RDONLY, 0777);
			 
            if (File > -1)
            {
                // get FileSize
                FileSize = sceIoLseek(File, 0, PSP_SEEK_END);
                sceIoLseek(File, 0, PSP_SEEK_SET);
            }

#else
            if (Filename.size() == 0) // bugfix posted by rt
            {
                File = 0;
                return;
            }

            File = fopen(Filename.c_str(), "rb");

            if (File)
            {
                // get FileSize
                fseek(File, 0, SEEK_END);
                FileSize = ftell(File);
                fseek(File, 0, SEEK_SET);
            }
#endif
        }



        //! returns name of file
        const c8* CReadFile::getFileName()
        {
            return Filename.c_str();
        }



        IReadFile* createReadFile(const c8* fileName)
        {
            CReadFile* file = new CReadFile(fileName);
            if (file->isOpen())
			{
                return file;
			}

            file->drop();

            return 0;
        }


    } // end namespace io
} // end namespace irr
